public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        //储存上一个节点的位置
        public ListNode next;
        //储存下一个节点的位置

        public ListNode(int val) {
            this.val = val;
        }
    }

    //获取双向链表的长度 和双向无关
    public ListNode head;
    public ListNode last;

    //获取链表的长度
    public int size() {
        int len = 0;
        ListNode cur = head;
        //遍历链表求长度
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;
    }

    //打印链表内容
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val);
            cur = cur.next;
        }
        System.out.println();
    }

    //查找某个关键字key是否在链表中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //如果链表为空 插入的同时初始化头节点和尾节点
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
    }


    public void addIndex(int index, int data) {
        int size = size();
        if (index < 0 || index > size) {
            throw new IndexOutOfBounds("双向链表index下标不合法：" + index);
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                //开始删除
                if (cur == head) {
                    //如果是头节点 删除头节点
                    head = head.next;
                    //如果删除后还有节点
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                        //此时head = head.next head已经为null
                        //只需要将last也置为null即可
                    }
                } else {
                    cur.prev.next = cur.next;
                    //将cur之前的节点next指向改为cur的下一个节点
                    //判断cur是否为尾节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                        //将后面节点的prev指向cur的prev
                    } else {
                        last = cur.prev;
                        //如果为最后一个节点 就把last向前移
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }
    public void removeAllKey(int key){
        ListNode cur = head;
        //逻辑和删除一个元素一模一样
        while (cur != null) {
            if (cur.val == key) {
                //开始删除
                if (cur == head) {
                    //如果是头节点 删除头节点
                    head = head.next;
                    //如果删除后还有节点
                    if (head != null) {
                        head.prev = null;
                    } else {
                        last = null;
                        //此时head = head.next head已经为null
                        //只需要将last也置为null即可
                    }
                } else {
                    cur.prev.next = cur.next;
                    //将cur之前的节点next指向改为cur的下一个节点
                    //判断cur是否为尾节点
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                        //将后面节点的prev指向cur的prev
                    } else {
                        last = cur.prev;
                        //如果为最后一个节点 就把last向前移
                    }
                }
            }
            cur = cur.next;
        }
    }

    public void clear(){
        ListNode cur = head;
        while (cur != null){
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}

